package com.gardenguell.salad.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.gardenguell.salad.R;
import com.gardenguell.salad.data.cache.MessageDBManager;
import com.gardenguell.salad.data.model.Vegetable;

import java.util.Collections;
import java.util.List;

/**
 * Created by absurd on 16/5/28.
 */
public class MessageAdapter extends ClickableRecyclerViewAdapter<MessageAdapter.ItemViewHolder> {

    private List<MessageDBManager.Message> mData = Collections.emptyList();

    public MessageAdapter(List<MessageDBManager.Message> data) {
        if(data != null)
            this.mData = data;
    }


    public void setData(List<MessageDBManager.Message> data){
        this.mData = data;
        notifyDataSetChanged();
    }


    @Override
    public MessageAdapter.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message, parent, false);
        view.setOnClickListener(this);
        return new ItemViewHolder(view);
    }


    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        MessageDBManager.Message msg = mData.get(position);
        Vegetable v = Vegetable.getVegetableFromId(msg.getVegetableId());
        holder.message.setText(msg.getInstanceId());
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class ItemViewHolder extends RecyclerView.ViewHolder{
        public ImageView vegetableImage;
        public TextView message;
        public TextView date;

        public ItemViewHolder(View itemView) {
            super(itemView);
            vegetableImage = (ImageView)itemView.findViewById(R.id.message_vegetable_image);
            message = (TextView) itemView.findViewById(R.id.message_tip);
            date = (TextView)itemView.findViewById(R.id.message_time);
        }
    }

}
